const { BASE_URL } = require('./utils/constant');
const express = require('express');
const path  = require('path');

const app = express();

// 使用 express 自带的中间件解析 JSON 和 URL-encoded 请求体
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 引入路由
const loginRouter = require('./router/login');
const userRouter = require('./router/user');
const productRouter = require('./router/product');
const productTypeRouter = require('./router/productType');
const orderRouter = require('./router/order');
const roleRouter = require('./router/role');
const travelGuideRouter = require('./router/travelGuide');
const shopCartRouter = require('./router/shopCart');

// 文件管理路由
const fileRouter = require('./router/file');


// 提供静态文件访问
app.use("/tempFiles", express.static(path.join(__dirname, 'tempFiles')));

app.get('/', (req, res) => {
    res.send('Hello World!')
})


app.post('/test', (req, res) => {
    let body = req.body;
    console.log(req.body);
    
    if (body.name) {
        body.name = body.name.toUpperCase();
    }

    if (body.age) {
        body.age = parseInt(body.age) + 1;
    }
    res.json(body);
})

app.use('/user', userRouter);
app.use('/auth', loginRouter);
app.use('/product', productRouter);
app.use('/productType', productTypeRouter);
app.use('/order', orderRouter);
app.use('/role', roleRouter);
app.use('/file', fileRouter);
app.use('/travelGuide', travelGuideRouter);
app.use('/shopCart', shopCartRouter);


app.listen(BASE_URL, () => {
    console.log('服务器已启动', `地址: localhost:${BASE_URL}`);
})